package com.py.credit.controller;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Application;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.ResultObj;
import com.py.credit.service.ApplicationService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.utils.UuidUtil;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 在线申请核查报告controller
 */
@Controller
@RequestMapping(value = "/application")
public class ApplicationController {

	@Autowired
	private ApplicationService applicationService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Value("${absolutepath}")
	private String absolutepath;

	/**
	 * 新增一个在线申请核查报告工作流
	 * @param applicant_unit 企业名称
	 * @param check_purpose 核查用途
	 * @param user_id 用户id
	 * @param username 用户名称
	 * @return
	 */
	@RequestMapping(value = "/add.do")
	@ResponseBody
	public Object add(@RequestParam(value = "applicant_unit", defaultValue = "", required = true) String applicant_unit,
			@RequestParam(value = "check_purpose", defaultValue = "", required = true) String check_purpose,
			@RequestParam(value = "user_id", defaultValue = "", required = true) String user_id,
					  @RequestParam(value = "username", defaultValue = "", required = true) String username) {
		//登录超时不让添加
		if(null==user_id || user_id.equals("") || user_id.equals("-1")){
			ResultObj resultObj = new ResultObj(ResultObj.ERR_NO_LOGIN, "登录超时");

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("resultObj", resultObj);
			return map;
		}

		String tybm=applicationService.selectTybmByQymc(applicant_unit);
		if(null==tybm || tybm.equals("")){
			ResultObj resultObj = new ResultObj(ResultObj.ERR_NO_LOGIN, "暂无企业数据");

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("resultObj", resultObj);
			return map;
		}

		Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String applicat_date = format.format(new Date());

		int id = applicationService.addApplicationInfo(applicant_unit,tybm,check_purpose,applicat_date,Integer.parseInt(user_id),username);
		String accessKey = UuidUtil.getInstance().getUUid();

		remoteBaseService.addWorkFlow("resourceApplication",accessKey,0+"",null,id+"",
				"“" + applicant_unit + "”核查报告申请","","7",null);

		ResultObj resultObj = new ResultObj(ResultObj.SUCCESS);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("resultObj", resultObj);
		return map;
	}

	/**
	 * 工作流页面获取信用核查报告详情
	 * @param taskId
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getActDetailView.json")
	public RestServiceResult<Map> getActDetailView(String taskId, HttpServletRequest request) {
		RestServiceResult<Map> result = new RestServiceResult<Map>();
		Map<String,Object> map = new HashMap<String, Object>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("ApplicationController#getActDetailView", "未获取到当前用户信息！");
				return result;
			}

			RestServiceResult<Integer> busId=remoteBaseService.getBusIdBuyTaskId(taskId);
			Application application=applicationService.selectApplicationInfo(busId.getData());
			map.put("application",application);
			result.setData(map);
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}
	
	/**
	 * 新增一个在线申请核查报告工作流
	 * @param applicant_unit 企业名称
	 * @param check_purpose 核查用途
	 * @param user_id 用户id
	 * @param username 用户名称
	 * @return
	 */
	@RequestMapping(value = "/addCheck.do")
	@ResponseBody
	public Object addCheck(@RequestParam(value = "qymc", defaultValue = "", required = true) String qymc,
			@RequestParam(value = "tyshxydm", defaultValue = "", required = true) String tyshxydm,
			@RequestParam(value = "fddbr", defaultValue = "", required = true) String fddbr,
			@RequestParam(value = "lxr", defaultValue = "", required = true) String lxr,
			@RequestParam(value = "lxrdh", defaultValue = "", required = true) String lxrdh,
			@RequestParam(value = "yx", defaultValue = "", required = true) String yx,
			@RequestParam(value = "purpose", defaultValue = "", required = true) String purpose,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<String> result = new RestServiceResult<String>();
		try {

			// 获取当前用户信息
			RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			Long userId=userInfo.getId();
			int id = applicationService.addCheck(qymc,tyshxydm,fddbr,lxr,lxrdh,yx,purpose,userId.intValue());
			String accessKey = UuidUtil.getInstance().getUUid();
			remoteBaseService.addWorkFlow("resourceApplication",accessKey,0+"",null,id+"",
					"“" + qymc + "”核查报告申请","","7",null);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("添加或修改核查报告异常");
			ExceptionLogger.error("添加或修改核查报告 异常！", e.getMessage(), e);
		}
		return result;
	}
}
